var _base = _ctx + "/rest/gen/${sname}/",
	cmd = {
		get: _base + "get",
		save: _base + "save",
		remove: _base + "delete",
		list: _base + "list"
	};

// 初始化
$(function() {
	// 初始化 表格
	$('#datagrid').datagrid({
		rownumbers: true,
		url: cmd.list,
		singleSelect: true,
		queryParams: formToObject('#searchForm'),
		loadFilter: WT.pageLoadFilter,
		columns: [
			[
			
			<#list fields as f>
					<#if f.pk>
						{field: '${f.simpleName}', hidden: true }<#rt/> 
					<#else>
						{field: '${f.simpleName}',title: '${f.name}', sortable:true }<#rt/>
					</#if><#if f_has_next><#lt/> ,</#if>
			</#list>
		 ]
		]
	});
	//处理dialog
			$('#dialog').dialog({
				title: '${name}',
				width: 500,
				closed: true,
				buttons:[{
					text:'保存',handler:function(){ 
						cmd_save();
					}
					},{text:'关闭', handler:function(){ $(this).dialog("close"); }
					}] 
			});
			//修正 DATA GRID 高度
$(window).resize((function() {
		$('#datagrid').datagrid('resize', {
			'height': $(window).height() - 36 -  $('.searchHead').height() - 9
		});
		return arguments.callee;
	})());

});

//function 


//添加
function cmd_insert() {
	$('#ff').form('clear').form("load", {
		//enabled: true 在此添加 新增的默认值 
	});
	$('#dialog').dialog("open");
}


//修改
function cmd_modify() {
	var row = $('#datagrid').datagrid("getSelected");
	if (row) {
		$('#dialog').dialog("open");
		<#if (pks?size>1 )>
			$('#ff') .form('clear').form({onBeforeLoad:function(target,param){
				<#list pks as p>
					param.${p.simpleName}=row.${p.simpleName};
				</#list>
			}}).form("load", cmd.get );
	<#elseif (pks?size>0 )>
			$('#ff') .form('clear').form("load", cmd.get+"/" + row.${pks[0].simpleName});
	<#else></#if>
		
} else
	$.messager.alert('提示', '请选择一条记录');
}
//删除
function cmd_delete() {
	var row = $('#datagrid').datagrid("getSelected");
	if (row) {
		$.messager.confirm('询问', '确定要删除[' + row.name + ']？', function(r) {
			if (r) {
				$.ajax({
					type: "delete",
						<#if (pks?size>1 )>
						
							url: cmd.remove,
							data:{
								<#list pks as p>
								 ${p.simpleName}:row.${p.simpleName}<#if p_has_next><#lt/> ,</#if>
							</#list>
							}
					<#elseif (pks?size>0 )>	
						url: cmd.remove +'/'+ row.${pks[0].simpleName},
						<#else>
					
						</#if>
					
					dataType: 'json',
					success: function(data) {
						if (err(data)) return;
						info('删除' + row.name + '成功！');
						$('#datagrid').datagrid("reload");
					}
				});
			}
		});

	} else $.messager.alert('提示', '请选择一条记录');
}
  

//保存
function cmd_save() {
	$('#ff').form('submit', {
		method: 'PUT',
		url: cmd.save,
		success: function(data) {
			if (err(data)) return;
			$('#datagrid').datagrid("reload");
			$('#dialog').dialog("close");
			info('保存成功！');
		}
	});
}
//查询
function  search() {
	var de=formToObject('.searchBox');
	$('#datagrid').datagrid("load", de);
}
	
	